package com.hgnu.dorm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.hgnu.dorm.domain.dto.UserPageQueryDto;
import com.hgnu.dorm.domain.po.PageBean;
import com.hgnu.dorm.domain.po.User;
import com.hgnu.dorm.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
@Tag(name = "用户管理接口")
@SaCheckRole("admin")
public class UserController {
    @Autowired
    private UserService userService;
    //分页查询
    @GetMapping
    @Operation(summary = "获取用户列表接口")
    public SaResult list(UserPageQueryDto dto){
        PageBean page = userService.page(dto);
        return SaResult.data(page);
    }
    /*根据id获取单个用户*/
    @GetMapping("/{id}")
    @Operation(summary = "根据id获取单个用户")
    public SaResult get(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return SaResult.data(user);
    }
    /*根据多个id删除用户*/
    @DeleteMapping("/{ids}")
    @Operation(summary = "根据多个id删除用户")
    public SaResult delete(@PathVariable("ids") Long[] ids){
        return SaResult.data(userService.delete(ids));
    }
    /*管理员添加用户*/
    @PostMapping
    @Operation(summary = "管理员添加用户")
    public SaResult add(@RequestBody User user){
        userService.insertUser(user);
        return SaResult.ok();
    }
    /*修改用户信息*/
    @PutMapping
    @Operation(summary = "修改用户信息")
    public SaResult updateInfo(@RequestBody User user){
        userService.updateUser(user);
        return SaResult.ok();
    }
    /*修改账号状态*/
    @PutMapping("/{id}")
    @Operation(summary = "修改账号状态")
    public SaResult updateStatus(@PathVariable("id") Integer id){
        userService.updateStatus(id);
        return SaResult.ok();
    }
}
